package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private RoomInfoService roomInfoService;
@Autowired
private GraphInfoService graphInfoService;
@Autowired
private ApartmentFacilityService apartmentFacilityService;
@Autowired
private ApartmentLabelService labelService;
@Autowired
private ApartmentFeeValueService feeValueService;
@Autowired
private LabelInfoMapper labelInfoMapper;
    @Override
    public void saveOrUpdateByMyself(ApartmentSubmitVo apartmentSubmitVo) {

        boolean isUpdate = apartmentSubmitVo.getId()!=null;
        super.saveOrUpdate(apartmentSubmitVo);
if(isUpdate){
    LambdaQueryWrapper<GraphInfo> graphQuery = new LambdaQueryWrapper<>();
    graphQuery.eq(GraphInfo::getItemId, apartmentSubmitVo.getId());
    graphQuery.eq(GraphInfo::getItemType, ItemType.APARTMENT);
    graphInfoService.remove(graphQuery);

    LambdaQueryWrapper<ApartmentFacility> facilityQuery = new LambdaQueryWrapper<>();
    facilityQuery.eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
    apartmentFacilityService.remove(facilityQuery);

    LambdaQueryWrapper<ApartmentLabel> labelQuery = new LambdaQueryWrapper<>();
    labelQuery.eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId());
    labelService.remove(labelQuery);

    LambdaQueryWrapper<ApartmentFeeValue> feeValueQuery = new LambdaQueryWrapper<>();
    feeValueQuery.eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVo.getId());
    feeValueService.remove(feeValueQuery);
}

        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if(!CollectionUtils.isEmpty(graphVoList)){
            List<GraphInfo> graphInfoList = new ArrayList<>();
        for (GraphVo graphVo : graphVoList) {
          GraphInfo graphInfo = new GraphInfo();
          graphInfo.setName(graphVo.getName());
          graphInfo.setUrl(graphVo.getUrl());
          graphInfo.setItemId(apartmentSubmitVo.getId());
          graphInfo.setItemType(ItemType.APARTMENT);
          graphInfoList.add(graphInfo);
        }
            graphInfoService.saveBatch(graphInfoList);
        }


        //插入配套信息
        //先通过前端传入参数Vo 获取该公寓的配套信息
        //然后将配套信息重新加载回去（这里不能凭空加载，需要创建关系对象，然后将对象插入回去）
        List<Long> facilityInfoIdList = apartmentSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIdList)){
            ArrayList<ApartmentFacility> facilityList = new ArrayList<>();
            for (Long facilityId : facilityInfoIdList) {
                ApartmentFacility apartmentFacility = new ApartmentFacility();
                apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
                apartmentFacility.setFacilityId(facilityId);
                facilityList.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(facilityList);
        }

        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if(!CollectionUtils.isEmpty(labelIds)){
            List<ApartmentLabel> apartmentLabelList = new ArrayList<>();
            for (Long labelId : labelIds) {
                ApartmentLabel apartmentLabel= new ApartmentLabel();
                apartmentLabel.setLabelId(labelId);
                apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
                apartmentLabelList.add(apartmentLabel);
            }
            labelService.saveBatch(apartmentLabelList);
        }

        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if(!CollectionUtils.isEmpty(feeValueIds)){
            List<ApartmentFeeValue> apartmentFeeValues= new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue= new ApartmentFeeValue();
                apartmentFeeValue.setApartmentId(apartmentSubmitVo.getId());
                apartmentFeeValue.setFeeValueId(feeValueId);
                apartmentFeeValues.add(apartmentFeeValue);
            }
            feeValueService.saveBatch(apartmentFeeValues);
        }


    }

    @Override
    public IPage<ApartmentItemVo> pageApartmentItemByQuery(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageItem(page,queryVo);
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        //查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        //查询图片列表信息
        List<GraphVo> graphVoList = graphInfoMapper.findgraphDetail(id,ItemType.APARTMENT);
        //查询标签列表信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.findFacilityDetail(id);
        //查询配套列表信息
        List<FeeValueVo> feeValueVoList = feeValueMapper.findFeeValueDetail(id);
        //查询杂费列表信息
        List<LabelInfo> labelInfoList = labelInfoMapper.findLabelDetail(id);
        ApartmentDetailVo apartmentDetailVo=new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        //这个方法可以让给定源Bean的属性复制到目标Bean中，甚至他们不需要有任何类型上的关系，只需要属性名相同即可。
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        return  apartmentDetailVo;
    }

    @Override
    public void removeById_Myself(Long id) {

        //删除公寓之前判断公寓是否还有房间
        LambdaQueryWrapper<RoomInfo> roomQueryWrapper = new LambdaQueryWrapper<>();
        roomQueryWrapper.eq(RoomInfo::getApartmentId,id);
        long count = roomInfoService.count(roomQueryWrapper);
        if(count>0)
            //终止删除，并返回提示信息
            throw new LeaseException(ResultCodeEnum.ADMIN_APARTMENT_CODE_ERROR);


        apartmentInfoMapper.deleteById(id);

        LambdaQueryWrapper<GraphInfo> graphQuery = new LambdaQueryWrapper<>();
        graphQuery.eq(GraphInfo::getItemId,id);
        graphQuery.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphInfoService.remove(graphQuery);

        LambdaQueryWrapper<ApartmentFacility> facilityQuery = new LambdaQueryWrapper<>();
        facilityQuery.eq(ApartmentFacility::getApartmentId,id);
        apartmentFacilityService.remove(facilityQuery);

        LambdaQueryWrapper<ApartmentLabel> labelQuery = new LambdaQueryWrapper<>();
        labelQuery.eq(ApartmentLabel::getApartmentId,id);
        labelService.remove(labelQuery);

        LambdaQueryWrapper<ApartmentFeeValue> feeValueQuery = new LambdaQueryWrapper<>();
        feeValueQuery.eq(ApartmentFeeValue::getApartmentId,id);
        feeValueService.remove(feeValueQuery);

    }
}




